﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;
using YDServices.Enum; 
using static YDCommon.Utils;

namespace YDWin.Factory
{
    public partial class AddFactoryForm : AddForm
    {
        FactoryService svr = new FactoryService();
        SystemParameterService sps = new SystemParameterService();
        public T_Factory editTu = new T_Factory();
        public string state = "";
        public AddFactoryForm()
        {
            InitializeComponent();
            //textedit多行显示
            txtAddress.Properties.AutoHeight = false;
            //从上开始
            txtAddress.Properties.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Top; 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
 
            T_Factory fact = new T_Factory();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            if (txtFactoryName.Text.Trim().Equals(""))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "厂家名称不能为空!\n"); 
                return;
            }

            fact.FactoryType = (int)cboFactoryType.EditValue;
            fact.FactoryName = txtFactoryName.Text.Trim();
            fact.Address = txtAddress.Text.Trim();
            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                svr.EditItem(fact);
            }
            else
            {
                svr.AddItem(fact);
            }
      

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "厂家信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddFactoryForm_Load(object sender, EventArgs e)
        {
            //订单分类
            var fDs = sps.getComboxList("OrderType");
            cboFactoryType.Properties.ValueMember = "Value";   //实际要用的字段
            cboFactoryType.Properties.DisplayMember = "Name";    //要显示的字段
            cboFactoryType.Properties.DataSource = fDs;
 

            if (!state.Equals("") && state.Equals("Edit"))
            {
                txtFactoryName.Text = editTu.FactoryName;
                txtAddress.Text = editTu.Address;
                cboFactoryType.EditValue = editTu.FactoryType;
            }
        }
 
    }
}